package com.settlement.system.service.csv;

import com.settlement.system.model.entity.common.TblCostWeightRecord;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblRrChannelService;
import com.settlement.system.common.util.common.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单稽核导出
 *
 * @author Ximi
 * @since 2020/10/24
 */
@Slf4j
public class TblCostWeightRecordCsvDatas extends BaseServiceCsvDatas {

    ITblRrChannelService tblRrChannelService;

    public TblCostWeightRecordCsvDatas(IBaseService baseService,
                                       Map<String, Object> params,
                                       ITblRrChannelService tblRrChannelService) {
        super(baseService, params);
        this.tblRrChannelService = tblRrChannelService;
    }

    @Override
    public Object beforeWrite(Object data) {
        if (data instanceof TblCostWeightRecord) {
            TblCostWeightRecord detail = (TblCostWeightRecord) data;
            String channelNo = detail.getChannelNo();
            if(StringUtils.isNotEmpty(channelNo)){
                String[] split = channelNo.split(",");
                List<String> collect = Arrays.asList(channelNo.split(",")).stream().map(s -> {
                    return tblRrChannelService.getNameByNo(s);
                }).collect(Collectors.toList());

                String channelName = StrUtil.toString(collect.toArray());
                detail.setChannelName(channelName);
            }

            return detail;
        }
        return data;
    }
}
